package com.office.automate.login.data.validator;

import com.office.automate.encrypt.password.EncryptPassword;
import com.office.automate.exception.authenticate.UserAuthenticationExcaptionKeys;
import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.model.authenticate.ChangePassword;
import com.office.automate.model.authenticate.UserAuthenticator;
import com.office.automate.validation.framework.BaseValidator;

public class LoginDataValidator extends BaseValidator {
	private void validateUsername(String aUsername) {
		if (aUsername == null || aUsername.isEmpty()) {
			addFieldValidationException("username", new FrontierVisibleException(UserAuthenticationExcaptionKeys.USERNAME_IS_EMPTY, null));
		}
	}

	private void validatePassword(String aPassword) {
		if (aPassword == null || aPassword.isEmpty()) {
			addFieldValidationException("password", new FrontierVisibleException(UserAuthenticationExcaptionKeys.PASSWORD_IS_EMPTY, null));
		}
	}

	private void validateCurrentPassword(String aSystemPassword, String aPassword) throws FrontierVisibleException {
		if (aPassword == null || aPassword.isEmpty()) {
			addFieldValidationException("CurrentPassword", new FrontierVisibleException(UserAuthenticationExcaptionKeys.CURRENT_PASSWORD_IS_EMPTY, null));
		} else {
			if (!aSystemPassword.equals(new EncryptPassword().getEncryptedPasswordMD5(aPassword))){
				addFieldValidationException("CurrentPassword", new FrontierVisibleException(UserAuthenticationExcaptionKeys.CURRENT_PASSWORD_IS_INCORRECT, null));
			}
		}
	}

	private void validateNewPassword(ChangePassword changePassword) {
		if (changePassword.getNewPassword() == null || changePassword.getNewPassword().isEmpty()) {
			addFieldValidationException("NewPassword", new FrontierVisibleException(UserAuthenticationExcaptionKeys.NEW_PASSWORD_IS_EMPTY, null));
		}
		if (changePassword.getConfirmNewPassword() == null || changePassword.getConfirmNewPassword().isEmpty()) {
			addFieldValidationException("ConfirmNewPassword", new FrontierVisibleException(UserAuthenticationExcaptionKeys.CONFIRM_PASSWORD_IS_EMPTY, null));
		}else if (!(changePassword.getNewPassword() == null || changePassword.getNewPassword().isEmpty()) && 
						!changePassword.getConfirmNewPassword().equals(changePassword.getNewPassword())) {
			addFieldValidationException("ConfirmNewPassword", new FrontierVisibleException(UserAuthenticationExcaptionKeys.NEW_AND_CONFIRM_PASSWORDS_ARE_INCORRECT, null));
		}
	}

	public void validateLoginDataFields (UserAuthenticator anUserAuthenticator) throws FrontierVisibleException {
		if (anUserAuthenticator != null) {
			// Validate Username
			validateUsername(anUserAuthenticator.getUsername());
			// Validate Password
			validatePassword(anUserAuthenticator.getPassword());
		} else {
			throw new FrontierVisibleException(UserAuthenticationExcaptionKeys.INVALID_AUTHENTICATOR, null);
		}
	}

	public void validateChangePasswordDetails(ChangePassword changePassword) throws FrontierVisibleException {
		validateCurrentPassword(changePassword.getCurrentSystemPassword(), changePassword.getCurrentPassword());
	
		validateNewPassword(changePassword);
	}
}
